/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年6月17日 下午7:07:51 
 * 
 */
package phoenics.common.util;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

/**
 *
 */
public class JacksonUtil {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(JacksonUtil.class);
	public static class Result{
		private String result="";
		private String message="";
		private int status=0;
		public Result(){}
		public Result(int status,String message,String result){
			this.message=message;
			this.status=status;
			this.result=result;
		}
		public String getResult() {
			return result;
		}
		public void setResult(String result) {
			this.result = result;
		}
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}
		public int getStatus() {
			return status;
		}
		public void setStatus(int status) {
			this.status = status;
		}
	}

	public static ObjectMapper objectMapper;

	/**
	 * 使用泛型方法，把json字符串转换为相应的JavaBean对象。
	 * (1)转换为普通JavaBean：readValue(json,Student.class)
	 * (2)转换为List,如List<Student>,将第二个参数传递为Student
	 * [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List
	 * 
	 * @param jsonStr
	 * @param valueType
	 * @return
	 */
	public static <T> T readValue(String jsonStr, Class<T> valueType)  {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}

		try {
			return objectMapper.readValue(jsonStr, valueType);
		}catch (UnrecognizedPropertyException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * json数组转List
	 * @param jsonStr
	 * @param valueTypeRef
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> toList(String jsonStr, Class<T> valueType){
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}

		try {
			JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, valueType);
			return (List<T>)objectMapper.readValue(jsonStr, javaType);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * json数组转Map
	 * @param jsonStr
	 * @param valueTypeRef
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> Map<String,T> toMap(String jsonStr, Class<T> valueType){
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}

		try {
			JavaType javaType = objectMapper.getTypeFactory().constructParametricType(Map.class, String.class,valueType);
			return (Map<String,T>)objectMapper.readValue(jsonStr, javaType);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * json数组转List,map
	 * @param jsonStr
	 * @param valueTypeRef
	 * @return
	 */
	public static <T> T readValue(String jsonStr, TypeReference<T> valueTypeRef){
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}

		try {
			return objectMapper.readValue(jsonStr, valueTypeRef);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/**
	 * 把JavaBean转换为json字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJSon(Object object) {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}

		try {
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	//fillResultString
	public static String fillResultString(int status,String message,String result) {
		return toJSon(new Result(status,message,result));
	}
	/*public static String update(String jsonStr, String key,Object o) {
		if (objectMapper == null) {
			objectMapper = new ObjectMapper();
		}
		try {
			JsonNode rootNode=objectMapper.readValue(jsonStr, JsonNode.class);
			JsonNode nameNode = rootNode.path(key);  
			nameNode.g
			return objectMapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}*/
}
